﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AxMOVIEPLAYERLib;
using System.Drawing;
using System.Threading;
namespace GOM_PLAYER.control_subtitle
{
    public class SubtitleHelper
    {
        private bool haveSub = false;
        private Subtitle sub;
        private int index = -1;
        private float posY;
        private float posX;
        private short fontSize;
        private short style;
        private string fontName;
        private bool fontBold;
        private bool fontItalic;
        private bool fontUnderline;
        private uint fontColor;
        private uint bgFontColor;
        private float alphaValue;
        private short scrollSpeed;
        private short alignText;
        private int abstratAddTime;
        private int currentTime;


        public const int MAXIMUN_SIZE_FONT = 50;
        public const int MINIMUN_SIZE_FONT = 10;
        public const float MAXIMUN_Y = 1.0F;
        public const float MINIMUN_Y = 0.0F;
        public const float MAXIMUN_X = 1.0F;
        public const float MINIMUN_X = 0.0F;
        public static int DEFAUL_SIZE_FONT = 30;
        public static float DEFAUL_X = 0.20F;
        public static float DEFAUL_Y = 0.90F;



        private void init()
        {
            this.posX = DEFAUL_X;
            this.posY = DEFAUL_Y;
            this.fontSize = (short)DEFAUL_SIZE_FONT;
            this.style = 0;
            this.fontName = "arial";
            fontBold = false;
            this.fontItalic = false;
            this.fontUnderline = false;
            this.fontColor = Color2Uint32(Color.FromArgb(255, 255, 0));
            this.bgFontColor = Color2Uint32(Color.FromArgb(0, 0, 0));
            this.alphaValue = 1;
            this.scrollSpeed = 500;
            this.alignText = 0;
            this.index = -1;
            this.abstratAddTime = 0;
            this.currentTime = 0;


        }

        public short FontSize
        {
            get { return fontSize; }
            set { fontSize = value; }
        }

        public float PosX
        {
            get { return posX; }
            set { posX = value; }
        }

        public float PosY
        {
            get { return posY; }
            set { posY = value; }
        }
        public SubtitleHelper(string filename)
        {
            this.sub = Subtitle.readXMLSubtitle(filename);
            if (sub != null && this.sub.Elements.Count >= 0)
            {
                this.haveSub = true;
                init();
            }
        }
        public void onPlaying(int time, AxMoviePlayer player)
        {
            this.currentTime = time;
            int timeToCheck = this.currentTime + this.abstratAddTime;
            if (!this.haveSub)
                return;
            //if (this.index + 1 < this.sub.Elements.Count && this.convert_To_Milisecons(this.sub.Elements[index + 1].Time) <= time)
            //{
            //    this.index++;
            //}
            while (this.index + 1 < this.sub.Elements.Count
                //                && this.convert_To_Milisecons(this.sub.Elements[index].Time) <= time
                && this.convert_To_Milisecons(this.sub.Elements[index + 1].Time) <= timeToCheck)
            {
                this.index++;
            }

            while (this.index - 1 >= 0
                //              && this.convert_To_Milisecons(this.sub.Elements[index].Time) >= time
                && this.convert_To_Milisecons(this.sub.Elements[index - 1].Time) >= timeToCheck)
            {
                this.index--;
            }

            //if (this.fontSize> 30)
            //{
            //    player.ScrollTextPage = 3;
            //}
            //else
            //{
            //    player.ScrollTextPage = 1;
            //}
            if (this.index >= 0 && this.index < this.sub.Elements.Count)
            {
                player.DrawText(this.posX, this.posY, this.style, this.sub.Elements[this.index].Context, this.fontSize, this.fontName, this.fontBold, this.fontItalic, this.fontUnderline, this.fontColor, this.bgFontColor, this.bgFontColor, this.alphaValue, this.scrollSpeed, this.alignText);
            }
        }
        public static uint Color2Uint32(Color clr)
        {

            int t;
            byte[] a;

            t = ColorTranslator.ToOle(clr);

            a = BitConverter.GetBytes(t);

            return BitConverter.ToUInt32(a, 0);

        }
        private int convert_To_Milisecons(string input)
        {
            int result = -1;
            string[] hms = input.Split(':');
            if (hms.Count() < 3)
                return result;
            result = int.Parse(hms[0]) * 60 * 60 + int.Parse(hms[1]) * 60 + int.Parse(hms[2]);
            return result;
        }
        public void nextSub()
        {
            if (this.index + 1 < this.sub.Elements.Count)
            {
                this.abstratAddTime = this.convert_To_Milisecons(this.sub.Elements[this.index + 1].Time) - this.currentTime;
            }

        }
        public void previousSub()
        {
            if (this.index - 1 >= 0)
            {
                this.abstratAddTime = this.convert_To_Milisecons(this.sub.Elements[this.index - 1].Time) - this.currentTime;
            }
        }
    }
}
